package com.athuahua.spzx.controller;


import com.athuahua.spzx.entity.SysUser;
import com.athuahua.spzx.dto.system.LoginDto;
import com.athuahua.spzx.vo.system.LoginVo;
import com.athuahua.spzx.vo.system.SysMenuVo;
import com.athuahua.spzx.vo.system.ValidateCodeVo;
import com.athuahua.spzx.service.SysMenuService;
import com.athuahua.spzx.service.SysUserService;
import com.athuahua.spzx.service.ValidateService;
import com.athuahua.spzx.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: huahua
 * @Date: 2024/7/21 0021
 * @Time: 11:21
 * @Description:  用户登录接口
 */
@Tag(name = "用户登录接口")
@RestController
@RequestMapping("/admin/system/index")
@Slf4j
public class IndexController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private ValidateService validateService;


    @Resource
    private SysMenuService sysMenuService;


    //生成图片验证码
    @GetMapping("generateValidateCode")
    @Operation(summary = "生成图片验证码")
    public Result<ValidateCodeVo> generateValidateCode(){
        log.info("生成图片验证码");
        ValidateCodeVo vo = validateService.generateValidateCode();


        return Result.success(vo);
    }

    //用户登录
    @Operation(summary = "登录的方法")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto) {
        log.info("登录请求");
        LoginVo loginVo = sysUserService.login(loginDto);


        return Result.success(loginVo);
    }

    //获取用户信息
    @GetMapping("getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result getUserInfo(){
        log.info("获取用户信息");
        SysUser sysUser = sysUserService.getUserInfo();
        return Result.success(sysUser);
    }

    //退出登录
    @GetMapping("logout")
    @Operation(summary = "退出登录")
    public Result logout(){
        log.info("退出登录");
        sysUserService.logout();
        return Result.success(null);
    }

    //查询用户可以操作的菜单
    @GetMapping("menus")
    @Operation(summary = "查询用户可以操作的菜单")
    public Result Menus(){

        List<SysMenuVo> list = sysMenuService.findMenusByUserId();

        return Result.success(list);
    }


}
